<?php
	class DanhMucSanPham
	{
		public $dm_ma;
		public $dm_ten;
		public $dm_mo_ta;
		public $dm_ma_danh_muc_cha;
		public $dm_ma_gian_hang;
		
		public static function DanhSachDanhMuc()
		{
			$query = "select * from danh_muc";
			$result = QuerySQL($query);
			$listdanhmuc = array();
			while($row = mysql_fetch_array($result))
			{
				$danhmuc = new DanhMucSanPham;
				$danhmuc->dm_ma = $row["dm_ma"];
				$danhmuc->dm_ten = $row["dm_ten"];
				$danhmuc->dm_mo_ta = $row["dm_mo_ta"];
				$danhmuc->dm_ma_danh_muc_cha = $row["dm_ma_danh_muc_cha"];
				//$danhmuc->dm_ma_gian_hang = $row["dm_ma_gian_hang"];
				array_push($listdanhmuc, $danhmuc);
			}
			StopConnection();
			return $listdanhmuc;
		}
		
		public static function CapNhatDanhMuc($dm_ma,$dm_ten)
		{
			$sqlString = "update danh_muc set 		
						dm_ten = '$dm_ten'
						WHERE dm_ma = '$dm_ma'";	
			QuerySQL($sqlString);
			StopConnection();
		}
		
		public static function XoaDanhMuc($dm_ma)
		{
			$list = DanhMucSanPham::DanhSachDanhMucCon($dm_ma);
			if(count($list) > 0)
			{
				return 1;
			}
			$list = SanPham::DanhSachSanPhamTheoDanhMuc($dm_ma);
			if(count($list) > 0)
			{
				return 1;
			}
			
			$sqlString = "delete from danh_muc
						WHERE dm_ma = '$dm_ma'";			
			QuerySQL($sqlString);
			StopConnection();
			return 0;

		}
		
		public static function ThemDanhMuc($dm_ten,$dm_ma_danh_muc_cha)
		{
			$sqlString = "insert into danh_muc (dm_ten,dm_ma_danh_muc_cha) 		
						values('$dm_ten',$dm_ma_danh_muc_cha)";	
			return InsertAndGetID($sqlString);
			StopConnection();
		}

		public static function DanhSachDanhMucCha()
		{
			$query = "select * from danh_muc where dm_ma_danh_muc_cha = 0 ";
			$result = QuerySQL($query);
			$listdanhmuc = array();
			while($row = mysql_fetch_array($result))
			{
				$danhmuc = new DanhMucSanPham;
				$danhmuc->dm_ma = $row["dm_ma"];
				$danhmuc->dm_ten = $row["dm_ten"];
				$danhmuc->dm_mo_ta = $row["dm_mo_ta"];
				$danhmuc->dm_ma_danh_muc_cha = $row["dm_ma_danh_muc_cha"];
				//$danhmuc->dm_ma_gian_hang = $row["dm_ma_gian_hang"];
				array_push($listdanhmuc, $danhmuc);
			}
			StopConnection();
			return $listdanhmuc;
		}
		
		public static function DanhSachDanhMucCon($madanhmuccha)
		{
			$query = "select * from danh_muc where dm_ma_danh_muc_cha = $madanhmuccha";
			$result = QuerySQL($query);
			$listdanhmuc = array();
			while($row = mysql_fetch_array($result))
			{
				$danhmuc = new DanhMucSanPham;
				$danhmuc->dm_ma = $row["dm_ma"];
				$danhmuc->dm_ten = $row["dm_ten"];
				$danhmuc->dm_mo_ta = $row["dm_mo_ta"];
				$danhmuc->dm_ma_danh_muc_cha = $row["dm_ma_danh_muc_cha"];
				//$danhmuc->dm_ma_gian_hang = $row["dm_ma_gian_hang"];
				array_push($listdanhmuc, $danhmuc);
			}
			StopConnection();
			return $listdanhmuc;
		}
		
		public static function ChiTietDanhMuc($madanhmuc)
		{
			$query = "select * from danh_muc where dm_ma = $madanhmuc";
			$result = QuerySQL($query);
			while($row = mysql_fetch_array($result))
			{
				$danhmuc = new DanhMucSanPham;
				$danhmuc->dm_ma = $row["dm_ma"];
				$danhmuc->dm_ten = $row["dm_ten"];
				$danhmuc->dm_mo_ta = $row["dm_mo_ta"];
				$danhmuc->dm_ma_danh_muc_cha = $row["dm_ma_danh_muc_cha"];
				//$danhmuc->dm_ma_gian_hang = $row["dm_ma_gian_hang"];
				return $danhmuc;
			}
		}
	}
?>
